{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#save model example\n",
    "import tensorflow as tf\n",
    "v1=tf.Variable(tf.constant(1.0,shape=[1]),name=\"v1\")\n",
    "v2=tf.Variable(tf.constant(2.0,shape=[1]),name=\"v2\")\n",
    "result=v1+v2\n",
    "\n",
    "init_op=tf.global_variables_initializer()\n",
    "saver=tf.train.Saver()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init_op)\n",
    "    saver.save(sess,\"/home/lilin/model/model.ckpt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from /home/lilin/model/model.ckpt\n",
      "[ 3.]\n"
     ]
    }
   ],
   "source": [
    "#another version to restore\n",
    "import tensorflow as tf\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    saver=tf.train.import_meta_graph(\"/home/lilin/model/model.ckpt.meta\")\n",
    "    saver.restore(sess,\"/home/lilin/model/model.ckpt\")\n",
    "    print(sess.run(tf.get_default_graph().get_tensor_by_name(\"add:0\")))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#one way to restore the model\n",
    "import tensorflow as tf\n",
    "v1=tf.Variable(tf.constant(1.0,shape=[1]),name=\"v1\")\n",
    "v2=tf.Variable(tf.constant(2.0,shape=[1]),name=\"v2\")\n",
    "\n",
    "result=v1+v2\n",
    "\n",
    "saver=tf.train.Saver()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    saver.restore(sess,\"/home/lilin/model/model.ckpt\")\n",
    "    print(sess.run(result))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#rename a variable\n",
    "v1=tf.Variable(tf.constant(1.0,shape=[1]),name=\"name=other-v1\")\n",
    "v2=tf.Variable(tf.constant(2.0,shape=[1]),name=\"name=other-v2\")\n",
    "\n",
    "saver=tf.train.Saver({\"v1\":v1,\"v2\":v2})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# another example about slidemean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "v=tf.Variable(0,dtype=tf.float32,name=\"v\")\n",
    "\n",
    "for variables in tf.all_variables():\n",
    "    print(variables.name)\n",
    "    \n",
    "ema=tf.train.ExponentialMovingAverage(0.99)\n",
    "maintain_averages_op=ema.apply(tf.all_variables())\n",
    "\n",
    "for variables in tf.all_variables():\n",
    "    print(variables.name)\n",
    "    \n",
    "saver=tf.train.Saver()\n",
    "with tf.Session() as sess:\n",
    "    init_op=tf.global_variables_initializer()\n",
    "    sess.run(init_op)\n",
    "    \n",
    "    sess.run(tf.assign(v,10))\n",
    "    sess.run(maintain_averages_op)\n",
    "    saver.save(sess,\"/home/lilin/model/model_01/model.ckpt\")\n",
    "    print(sess.run([v,ema.average(v)]))\n",
    "    #expect out is:[10.0,0.99999905]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v=tf.Variable(0,dtype=tf.float32,name=\"v\")\n",
    "\n",
    "saver=tf.train.Saver({\"v/ExponentialMovingAverage\":v})\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    saver.restore(sess,\"/home/lilin/model_01/model.ckpt.meta\")\n",
    "    print(sess.run(v))\n",
    "    #expect out is:0.99999905"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "v=tf.Variable(0,dtype=tf.float32,name=\"v\")\n",
    "ema=tf.train.ExponentialMovingAverage(0.99)\n",
    "\n",
    "print(ema.variables_to_restore())\n",
    "\n",
    "saver=tf.train.Saver(ema.variables_to_restore())\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    saver.restore(sess,\"/home/lilin/model_01/model.ckpt\")\n",
    "    print(sess.run(v))\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#\n",
    "import tensorflow as tf\n",
    "from tensorflow.python.framework import graph_util\n",
    "\n",
    "v1=tf.Variable(tf.constant(1.0,shape=[1]),name=\"v1\")\n",
    "v2=tf.Variable(tf.constant(2.0,shape=[1]),name=\"v2\")\n",
    "\n",
    "result=v1+v2\n",
    "\n",
    "init_op=tf.initialize_all_variables()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init_op)\n",
    "    \n",
    "    graph_def=tf.get_default_graph().as_graph_def()\n",
    "    \n",
    "    output_graph_def=graph_util.convert_variables_to_constants(sess,graph_def,['ass'])\n",
    "    \n",
    "    with tf.gfile.GFile(\"/home/lilin/model_01/combined_model.pb\",\"wb\") as f:\n",
    "        f.write(out_graph_def.SerializerToString())\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#\n",
    "import tensorflow as tf\n",
    "from tensorflow.python.platform import gfile\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    model_filename=\"/home/lilin/model/model_01/combined_model.pb\"\n",
    "    \n",
    "    with gfile.FastGfile(model_filename,'rb') as f:\n",
    "        graph_def=tf.GraphDef()\n",
    "        graph_def.ParseFromString(f.read())\n",
    "        \n",
    "        result=tf.import_graph_def(graph_def,return_elements=[\"add:0\"])\n",
    "        print(sess.run(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
